package com.chijun.mform.create.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chijun.mform.FormBuilder;
import com.chijun.mform.create.mode.FormConfig;
import com.chijun.mform.R;
import com.chijun.mform.create.adapter.SelectFormFieldShowAdapter;
import com.chijun.mform.create.dialog.ISelectField;
import com.chijun.mform.create.dialog.SelectFieldDialog;
import com.chijun.mform.create.field.Field;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.example.ccj.b.c.custom.MImageView;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.b.f.BaseFragment;
import com.example.ccj.u.GenerateId;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 表单添加字段fragment
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/11/16 19:32
 */
public class FormAddFieldFragment extends BaseFragment{

    BaseRecycler<FormField> contentField;
    MImageView mIvSelectField;
    private List<FormField> formFieldList;
    //字段的选项
    private Map<Long,List<FormFieldOption>> formFieldOptionMap;


    FormConfig formConfig;

    public FormAddFieldFragment(FormConfig formConfig){
        this.formConfig = formConfig;

        this.formFieldList = formConfig.getFormFieldList();
        this.formFieldOptionMap = formConfig.getFormFieldOptionMap();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_form_select_field,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        contentField = view.findViewById(R.id.activity_form_select_field_rec);
        mIvSelectField = view.findViewById(R.id.activity_form_select_field_add_field);

        mIvSelectField.setOnClickListener(selectField);

        contentField.setLayoutManager(new LinearLayoutManager(getContext()));
        contentField.setAdapter(formFieldList,new SelectFormFieldShowAdapter(formConfig));
    }

    /**
     * 添加字段
     */
    View.OnClickListener selectField = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SelectFieldDialog selectFieldDialog = new SelectFieldDialog(v.getContext(), FormBuilder.getInstance().getFields(), new ISelectField() {
                @Override
                public void complete(Field field) {
                    //根据返回的字段类型创建字段
                    long id = GenerateId.getId();
                    FormField formField = new FormField();
                    formField.setId(id);
                    formField.setName(field.name);
                    formField.setType(field.id);
                    formField.setHint(field.hint);
                    formField.setFormId(formConfig.getForm().id);

                    //图片字段默认可以选择10张图片
                    if (field.id == 10) {
                        formField.setLittleNum(10);
                    }

                    //选项字段添加默认选项
                    if (field.id == FormField.DAN_XUAN || field.id == FormField.DUO_XUAN) {
                        List<FormFieldOption> formFieldOptionList = new ArrayList<>();
                        for (int i = 0; i < 3; i++) {
                            FormFieldOption option = new FormFieldOption(GenerateId.getId(),id,"选项" + (i+1));
                            formFieldOptionList.add(option);
                        }
                        formFieldOptionMap.put(id,formFieldOptionList);
                    }

                    //将字段放入容器
                    contentField.addDataOnTail(formField);
                }
                @Override
                public void cancel() {}
            });
            selectFieldDialog.show();
        }
    };


}
